<?php
class ApiAction extends Action{
    public function index(){
      $this->display(); 

    }
    public function page(){
      $this->display(); 

    }
    public function mapList(){
      $res = M('map')->select();
      $res1 =array('code' =>1000,'data'=>$res);
      $this->ajaxReturn($res1,'json');

    }
    public function mapAdd(){
      $data=[];
      $data['lng']     = $_POST['lng'];
      $data['lat']     = $_POST['lat'];
      $data['content'] = $_POST['text'];
      $data['city']    = $_POST['city'];
      $data['time']    = time();
      $map = M('map')->add($data);
      if ($map) {
        $res =array('code' =>1000,'info'=>'成功！');
      }else{
        $res =array('code' =>1001,'info'=>'添加失败！');
      }

      

      $this->ajaxReturn($res,'json');

    }
    //传递s、t字符串  默认有值
    public function q1(){
      $s = $_REQUEST['str_s'];
      $t = $_REQUEST['str_t'];
      if (empty($t) && empty($s)) {
        $s = 'This is a text for C programming';
        $t = 'This is C programming text';
        $second_word = $this->getSecondWord($s, $t);
        echo '第⼆⻓公共单词为：', $second_word;
      }else{

        $second_word = $this->getSecondWord($s, $t);
        echo '第⼆⻓公共单词为：', $second_word;
      }
      
     

    }
    //支持传递多参数  逗号隔开
    public function q2(){
      $num_arr = explode(',',$_GET['num']);
      if (empty($_GET['num'])) {
        $this->printing(15);
        echo '-----','<br>';
        $this->printing(22);
        echo '-----','<br>';
        $this->printing(16);    
      }else{
        foreach ($num_arr as $v) {
            $this->printing($v);
            echo '-----','<br>';
        }
      }
      

      

    }
    public function getSecondWord($s, $t)
    {
        // 字符串不为空
        if (empty($s)) return null;
        if (empty($t)) return null;
        // 1. 字符串变数组
        $arr1 = explode(' ', $s);
        $arr2 = explode(' ', $t);
        // 至少两个单词
        if (count($arr1) < 2) return null;
        if (count($arr2) < 2) return null;
        // 2. 去重，省去多余时间
        $arr1 = array_unique($arr1);
        $arr2 = array_unique($arr2);
        // 3. 排序，按单词长度降序排序
        usort($arr1, 'compare');
        usort($arr2, 'compare');
        // 相同返回第二个
        if ($arr1 == $arr2) return $arr1[1];
        // 4. 返回出字符串最长的前两个
        $arr = [];
        foreach ($arr1 as $k => $v) {
            foreach ($arr2 as $kk => $vv) {
                if ($v == $vv) {
                    $arr[] = $v;
                }
            }
        }
        if (count($arr) > 1) {
            return $arr[0];
        } else {
            return null;
        }
    }
     
    // 比较函数
    public function compare($a, $b)
    {
        if (strlen($a) == strlen($b)) {
            return 0;
        }
        return (strlen($a) < strlen($b)) ? 1 : -1;
    }


    //输出
    public function printing($num)
    {
        if (floor($num)!=$num || $num>10000){
            echo '请输入 N <= 10000 的整数!';
            die;
        }
        $arr = $this->decompose($num);
        if ($arr == 'NONE') {
            echo 'NONE';
            die;
        }
        echo '分解组合如下：';
        $group = [];
        foreach ($arr as $v) {
            $group[] = range($v[0], $v[1] + $v[0] - 1);
        }
        echo '（共', count($group), '组）';
        echo '<br>';
        foreach ($group as $k => $v) {
            echo '第' . ($k + 1) . '组：';
            foreach ($v as $vv) {
                echo $vv . ' ';
            }
            echo '<br>';
        }
    }
    // 分解过程
    public function decompose($num)
    {
        $arr = [];
        if ($num < 0) {
            // 负整数处理
            for ($i = -1; $i > $num; $i--) {
                for ($j = 2; $j < abs($num); $j++) {
                    if (2 * $num == (2 * $i + $j - 1) * $j) {
                        $arr[] = [$i, $j];
                    }
                }
            }
        } else {
            // 整数处理
            for ($i = 1; $i < $num; $i++) {
                for ($j = 0; $j < $num; $j++) {
                    if (2 * $num == (2 * $i + $j - 1) * $j) {
                        $arr[] = [$i, $j];
                    }
                }
            }
        }
        if (empty($arr)) {
            return 'NONE';
        } else {
            return $arr;
        }
    }



}
